package com.lagou.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsConfig {

    @Value("${spring.elasticsearch.rest.uris}")
    private String hostList;

    @Bean
    public RestHighLevelClient client() {
        String[] hostArr = hostList.split(",");
        HttpHost[] httpHostArray = new HttpHost[hostArr.length];
        for(int i=0;i<hostArr.length;i++) {
            String host = hostArr[i];
            httpHostArray[i] = new HttpHost(host.split(":")[0],
                    Integer.parseInt(host.split(":")[1]));
        }
        return new RestHighLevelClient(RestClient.builder(httpHostArray));
    }
}
